package com.example.searchengine.service;

import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

//@Service
public class Parser {
    private static final String INPUT_PATH = "G:/javaAPI/docs/api/";
    private static final String OUTPUT_PATH = "G:/raw_data.txt";
    //通过myInit方法完成整个预处理流程
    //@PostConstruct
    public static void myInit(){
        //1.枚举出 INPUT_PATH 文件下的所有html文件(递归)
        ArrayList<File> fileList = new ArrayList<>();
        enumFile(INPUT_PATH,fileList);
        //2.针对枚举出来的html文件进行遍历,依次打开每个文件,并读取内容
        //把内容转换成需要的结构化数据(DocInfo 对象)
        StringBuilder builder = new StringBuilder();
        long startTime = System.currentTimeMillis();
        try(FileWriter writer = new FileWriter(OUTPUT_PATH)){
            for (File file:fileList) {
                //输出要转换的文件
//            System.out.println("trans: " + file.getAbsolutePath() + "...");
                String row = trans(file);
                //builder.append(row);
                //3.把 DocInfo 对象写入到最终的输出文件种(OUTPUT_PATH). 写成行文本的形式
                writer.write(row);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("==============================");
        System.out.println(endTime - startTime);
        System.out.println("==============================");
    }

    private static String trans(File file) {
        //1.转换标题 title
        String title = transTitle(file);
        //2.转换url
        String url = transUrl(file);
        //3.转换内容
        String count = transCount(file);
        return title + "\3" + url + "\3" + count + "\n";
    }

    private static String transCount(File file) {
        try(FileReader fileReader = new FileReader(file)) {
            boolean isCount = true;
            StringBuilder stringBuilder = new StringBuilder();
            while (true){
                int ret = fileReader.read();
                if (ret == -1){
                    break;
                }
                char c = (char) ret;
                if(isCount){
                    if (c == '<'){
                        isCount = false;
                        continue;
                    }
                    if (c == '\n' || c == '\r'){
                        c = ' ';
                    }
                    stringBuilder.append(c);
                }else {
                    if (c == '>'){
                        isCount = true;
                    }
                }
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String transUrl(File file) {
        //在线官方文档路径
        //https://docs.oracle.com/javase/8/docs/api/  index.html
        //本地路径
        //G:/javaAPI/docs/api/index.html
        String pathOne = "https://docs.oracle.com/javase/8/docs/api/";
        String pathTwo = file.getAbsolutePath().substring(INPUT_PATH.length());
        return pathOne + pathTwo;
    }

    private static String transTitle(File file) {
        String name = file.getName();
        return name.substring(0,name.length() - ".html".length());
    }

    private static void enumFile(String inputPath, ArrayList<File> fileList) {
        File file = new File(inputPath);
        File[] files = file.listFiles();
        for (File file1:files) {
            if (file1.isDirectory()){
                enumFile(file1.getAbsolutePath(),fileList);
            }else if (file1.getAbsolutePath().endsWith(".html")){
                fileList.add(file1);
            }
        }
    }
}
